package com.lizard.schedule.persistence.sharedpreferences;

import android.content.Context;
import android.content.SharedPreferences;

import com.lizard.schedule.ScheduleApplication;

/**
 * 记录最后一次同步时间SharedPreference
 */
public class SyncTimePref {

    private static SyncTimePref instance;

    private SharedPreferences mPref;

    /**
     * pref文件名
     */
    private final static String PREF_NAME = "pref_b";

    /**
     * 最后一次同步时间
     */
    private final static String PARAM_LAST_SYNC_TIME = "p_a";

    private SyncTimePref() {
        mPref = ScheduleApplication.applicationContext
                .getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static SyncTimePref getInstance(){
        if(instance == null){
            synchronized (SyncTimePref.class){
                if(instance == null){
                    instance = new SyncTimePref();
                }
            }
        }
        return instance;
    }

    /**
     * 记录最后一次同步时间
     *
     * @param username
     * @param time
     */
    public void setLastSyncTime(String username, long time){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putLong(PARAM_LAST_SYNC_TIME + username, time);
        editor.commit();
    }

    /**
     * 获取最后一次同步时间
     *
     * @param username
     * @return
     */
    public long getlastSyncTime(String username){
        return mPref.getLong(PARAM_LAST_SYNC_TIME + username, 0);
    }

}
